package com.xzc.rocketmq.base.producer;

import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.common.RemotingHelper;

import java.util.concurrent.TimeUnit;

public class OneWayProducer {
    public static void main(String[] args) throws Exception {
        //1 实例化消息生产者Producer
        DefaultMQProducer producer = new DefaultMQProducer("group1");
        //2 设置NameServer的地址 这个地方的主机名必须与broker里面的配置一致，不然会报TOPIC无法自动生成
        producer.setNamesrvAddr("rocketmq-nameserver1:9876;rocketmq-nameserver2:9876");
        //3 启动Producer实例
        producer.start();

        for (int i = 0; i < 10; i++) {
            //4 创建消息，并指定Topic，Tag和消息体
            Message msg = new Message("TopicTest" /* Topic */,
                    "TagOneWay" /* Tag */,
                    ("Hello RocketMQ " + i).getBytes(RemotingHelper.DEFAULT_CHARSET) /* Message body */
            );
            //5 发送消息到一个Broker
            producer.sendOneway(msg);

            TimeUnit.SECONDS.sleep(1);
        }
        //6 如果不再发送消息，关闭Producer实例。
        producer.shutdown();
    }
}
